//
//  DrawRectView.swift
//  L02DrawShapes
//
//  Created by liy on 2024/12/24.
//

import UIKit

class DrawRectView: UIView {
  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  override func awakeFromNib() {
    super.awakeFromNib()
  }

  override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    // 画多个矩形
    context?.addRects([CGRectMake(20, 20, 50, 50),
                       CGRectMake(40, 40, 50, 50),
                       CGRectMake(50, 50, 90, 90)])
    context?.addRect(CGRectMake(150, 150, 200, 200))
    context?.setFillColor(UIColor.red.cgColor)
    context?.fillPath()

    // 画边框（线条）
		//为何此处还要重新添加一条路径？
		//因为cg是基于路径的，fillPath和strokePath是独立的操作，fillPath填充后不会保留path
    context?.addRect(CGRectMake(150, 150, 200, 200))
    context?.setLineWidth(15)
    context?.setStrokeColor(UIColor.blue.cgColor)
    context?.strokePath()
  }
}
